

var LineGradientParser = {}

LineGradientParser.parse = function (mainEle) {
    let id = '';
    let x1 = 0;
    let y1 = 0;
    let x2 = 0;
    let y2 = 0;
    let xlink = '';
    let transform = '';
    let gradientUnits = '';
    let stopArr = [];
    
    const attributes = mainEle.attributes;
    for (let i = 0; i < attributes.length; i++) {
        const attr = attributes[i];
        if (attr.name == 'id') {
            id = attr.value;
        }
        else if (attr.name == 'x1') {
            x1 = parseFloat(attr.value);
        }
        else if (attr.name == 'y1') {
            y1 = parseFloat(attr.value);
        }
        else if (attr.name == 'x2') {
            x2 = parseFloat(attr.value);
        }
        else if (attr.name == 'y2') {
            y2 = parseFloat(attr.value);
        }
        else if (attr.name == 'xlink:href') {
            xlink = attr.value.replace('#','');
        }
        else if (attr.name == 'gradientUnits') {
            gradientUnits = attr.value;
        }
        else if (attr.name == 'gradientTransform') {
            transform = attr.value;
        }
    }

    const childEles = mainEle.children;
    for (let i = 0; i < childEles.length; i++) {
        const childEle = childEles[i];
        let tagName = childEle.tagName;
        if (tagName == 'stop') {
            let stopInfo = this.parseStop(childEle);
            if (stopInfo){
                stopArr.push(stopInfo);
            }
        }
    }
    let lineGrapdientInfo = {id,x1,y1,x2,y2,stopArr,xlink,transform,gradientUnits};
    return lineGrapdientInfo;
}

LineGradientParser.parseStop = function(mainEle){
    let stopInfo = {id:'',color:'',offset:0,opacity:1};
    const attributes = mainEle.attributes;
    for (let i = 0; i < attributes.length; i++) {
        const attr = attributes[i];
        if (attr.name == 'id') {
            stopInfo.id = attr.value;
        }
        else if (attr.name == 'stop-color') {
            stopInfo.color = attr.value;
        }
        else if (attr.name == 'stop-opacity') {
            stopInfo.opacity = parseFloat(attr.value);
        }
        else if (attr.name == 'offset') {
            stopInfo.offset = parseFloat(attr.value);
        }
    }
    return stopInfo;
}



export { LineGradientParser }